---
title: "Tunnels of attention: reconsidering issue competition"
subtitle: "Replication material"
author: "Emiliano Grossman & Isabelle Guinaudeau"
format: 
  html : default
editor: visual
lang: en
title-block-banner: "darkred"
title-block-banner-color: "white"
date: 2024-10-04
date-format: "D MMM YYYY"
toc: true
number-sections: false
fig-cap-location: top
execute: 
  echo: true
  warning: false
embed-resources: true
smooth-scroll: true
---

This is the replication material for the article "Tunnels of attention", published in *Comparative Political Studies.* Most ot the analyses are contained in this file, but we "source" one additional script, dedicated to the analysis of issue ownership in Appendix 6b.

## Packages and data

This chunk loads all the relevant R packages and datasets. There is several different datasets:

1.  data_eb : a dataset pairing party attention data with relevant Eurobarometer "Most important problem" data

2.  data_complete: the dataset with complete party attention data across eight countries.

3.  data_complete_with_early: same as 2. but limited to the four countries for which we collected the manifesto publication dates.

4.  ms: manifesto_publication dates.

5.  media_data: this dataset contains media data for France and Germany, used in the analysis in Appendix A13.

6.  mip_stats: summary data on the availability of MIP data in the Eurobarometer for Appendix A3

7.  media_wealth_germany: media data on the German Wealth tax used for figure 5 and appendix 14

Three other - relatively - minor datasets are loaded in Appendix A1 and Appendix A6b.

```{r}
### packages and data

library(tidyverse)
library(readr)
library(fixest)
library(ggpubr)
library(ggeffects)
library(modelsummary)
library(sandwich)
library(lmtest)
library(psych)
library(lavaan)
library(lavaanPlot)
library(readxl)
library(semPlot)
library(kableExtra)
library(DiagrammeRsvg)
library(zoo)
library(readxl)
library(margins)
library(ggrepel)

### data




deb = read_csv("data_eb.csv") # data for Eurobarometer MIP analyses
dc = read_csv("data_complete.csv") # complete data
dc_early = read_csv("data_complete_with_early.csv") # complete data with data on first manifesto published for 4 countries
ms= read_excel("manifesto_dates_data.xlsx") # manifesto publication dates 
load("media_data.Rdata")
load("mip_stats.Rdata")
mw = read.csv("media_wealth_germany.csv")




```

## Appendix A1: Party data presentation

```{r}
ps = read_csv("stats.csv")
ps = ps[,-1]
ps$Country = toupper(ps$Country)


gridExtra::grid.table(ps,rows = NULL)

```

## Figure 1: descriptive

```{r}


deb %>% filter(topic==107) %>% 
  filter(cn!="SE") %>% 
  group_by(cn,year = year) %>% 
  summarize(party = mean(party_share,na.rm=T),
                                  MIP = mean(mip_share,na.rm=T)) %>%
  dplyr::select(cn,year, party ,MIP) %>%
  na.omit() |>
  pivot_longer(names_to = "name",values_to = "value",-c('cn','year')) %>%
  
  ggplot(aes(year,value,linetype = name)) + geom_point() +
  geom_step() + theme_minimal() +
  theme(legend.title = element_blank(),legend.position = "bottom") +
  facet_wrap(.~cn) + labs(x="",y="share of attention") 


```

## Appendix A3: Eurobarometer MIP data per country

```{r}



ttf = gridExtra::ttheme_default(core=list(fg_params=list(hjust=0, x=0.01)))
 
 test %>% select(Country,Year = year) %>% 
 gridExtra::grid.table(theme=ttf,rows = NULL)

```

## Appendix A4: models underlying figure 2

```{r}
dc$year_country = paste(dc$year,dc$country_name, sep = "_")

 cns = unique(dc$cn)
 cns = cns[cns!="sw"]

 
 res2 =  
   models = lapply(cns, function(x){
     form = party_share ~ party_share_last + attention_others|year_country  
     form = feols(form,data = dc %>% filter(cn==x))
     form
   })
 
 res2 = res2[c(1:7)]
 
 names(res2) = c("Belgium","Denmark","France","Germany","Italy","Netherlands",
                 "United Kingdom")
 modelsummary::modelsummary(res2 , estimate = "{estimate}{stars}",
                            statistic = NULL,
                            #coef_omit = c(-1,-2,-3,),
                            coef_rename = c("(Intercept)" = "Constant",
                                            "party_share_last"="Stability",
                                            "attention_others"="Overlap"),
 #                                           "year" = "Year (trend)"),
                            #vcov = "robust", 
                            cluster = ~party,
                            gof_omit = 'DF|Deviance|AIC|BIC')
 
```

## Figure 2 : country stability and overlap

```{r}
res2out = lapply(res2,function(x) summary(x)$coeftable[1:2,1:2])
 
 test = data.frame(do.call(rbind,res2out))
 test$var2 = c("Stability","Overlap")
 test$cn = rep(c("Belgium","Germany","France","United Kingdom","Denmark",
                 "Netherlands","Italy"),each=2)
 

 test %>% #filter(cn != "sw") %>%
   ggplot(aes(cn,Estimate,color=var2)) + 
   geom_pointrange(aes(ymin=Estimate-Std..Error,ymax=Estimate+Std..Error),
                   position = position_dodge(width=.5)) +
   coord_flip() + geom_hline(yintercept = 0, linetype =2) +
   labs(x="",y="coefficients") + theme_minimal() + 
   scale_color_manual(values = c("black","gray")) +
   theme(legend.title = element_blank(),legend.position = "bottom")

```

## Appendix A4b: models with trend variable instead of election fixed effects

```{r}
res2 =  
  models = lapply(cns, function(x){
   form = party_share ~ party_share_last + attention_others + (year) 
   form = lm(form,data = dc %>% filter(cn==x))
   form
 })
 
 res2 = res2[c(1:7)]
 
names(res2) = c("Belgium","Denmark","France","Germany","Italy","Netherlands",
                "United Kingdom")


modelsummary::modelsummary(res2 , estimate = "{estimate}{stars}",
                            statistic = NULL,
                            #coef_omit = c(-1,-2,-3,),
                           coef_rename = c("(Intercept)" = "Constant",
                                           "party_share_last"="Stability",
                                           "attention_others"="Overlap",
                                           "year" = "Year (trend)"),
                            vcov = "robust",# cluster = ~party,
                            gof_omit = 'DF|Deviance|AIC|BIC')
 

```

## Appendix A5 : government status

```{r}

cabtest = function(x){
   dn = deb %>% filter(cn == x)  
   dn$cabinet_party = factor(dn$cabinet_party)
   #test1 = lm(party_share ~ party_share_last * cabinet_party + scale(p2_share)  + scale(share) + party , data = dn )
   test2 = lm(party_share ~ party_share_last + attention_others  * cabinet_party + mip_share  , data = dn )
   #out1 = ggeffect(test1,terms=c("party_share_last","cabinet_party"))
   #out1$var = "party_share_last"
   out2 = ggpredict(test2,terms=c("attention_others[0,.1,]","cabinet_party"))
   #out2$var = "p2_share"
   out = out2 #bind_rows(out1,out2)
   out$cn=x
   out
 }
 #data$cabinet_party[is.na(data$cabinet_party)] = 0
 cns= unique(deb$cn)
 cns=cns[cns!='SE']
 out = lapply(cns,cabtest)
 out2 = do.call(rbind,out)
 
 
 
 out2 %>% filter(x==.1) %>% filter(cn!="SE") %>%
   ggplot(aes(cn,predicted,color = group)) + geom_hline(yintercept = 0,linetype = 2) +
   geom_pointrange(aes(ymin=conf.low,ymax=conf.high),position = position_dodge(width=.3)) +
   theme_minimal() + scale_color_grey() + coord_flip() + 
   labs(x="Attention others",y = "Attention at present election", color = "Incumbent cabinet member") + 
   theme(legend.position = "bottom")
 
 
```

## Appendix A6b: testing for the effect of perceived issue ownership

As this document is already very long, we created additional scripts for some of the robustness checked that were added during the review process.

```{r}
source("issue_ownership.R")

out2 |> mutate(io = factor(io)) |>
  dplyr::filter(factor != "io") |>
  ggplot(aes(io,AME,color = vars)) +
  geom_pointrange(aes(ymin = AME-SE,ymax=AME+SE),position = position_dodge(.3)) +
  theme_light() + labs(x="Issue ownership",color = "") + facet_wrap(. ~ cns) +
  theme(legend.position = "bottom")

```

## Appendix A7: differenced dependent variable

```{r}
dcd = dc |> group_by(cn,year,party) |>
   mutate(attention_others_last = lag(attention_others),
          attention_others_diff = attention_others - attention_others_last,
          party_share_last2 = lag(party_share,2),
          party_share_diff = party_share - party_share_last,
          party_share_diff2 = party_share_last - party_share_last2)
 
 out = lapply(cns,function(x){
   #data$econ = ifelse(data$topic2 %in% c(1,103,105,107,15),1,0)
   fit =  lm(party_share_diff ~ (party_share_diff2) +  attention_others_diff,
             #+ scale(share) + factor(topic) 
             data= dcd  %>% filter(cn == x))
   #test = coeftest(fit,vcov = vcovCL, cluster = ~party)
   #tcofs = data.frame(test[c(2:4),c(1,2)])
   #  cofs= data.frame(summary(test)$coeftable)[c(2,3),c(1,2)]
   #cofs=data.frame(vars=rownames(tcofs),tcofs,country=x) 
   #cofs
   names(test) = x
   fit
 })
 
 names(out) = cns
 modelsummary(out,estimate = "{estimate}{stars}",
              statistic = NULL,
              #coef_omit = c(-1,-2,-3,),
              coef_rename = c("(Intercept)" = "Constant",
                              "party_share_diff2"="Stability (diff.)",
                              "attention_others_diff"= "Overlap (diff.)"),
              vcov = vcovCL, cluster = ~party,
              gof_omit = 'DF|Deviance|AIC|BIC')
 
 test = do.call(rbind,out)
 test$var2 = c("Stability (fixed)","Overlap (diff)", "Overlap (fixed)")
 test$cn = rep(c("Belgium","Germany","Denmark","France","Italy",
                 "Netherlands","United Kingdom"),each=3)
 
```

## Appendix A8: topic and country

```{r}

tcoeffs<-function(df=data,topic = topic,cn=cn){
  df2 = df #%>% filter(cn=="BE")
  reg<-function(x){ 
    fit = lm(party_share ~ party_share_last + attention_others, 
                                data=df2[df2$topic==x,])
   # test = coeftest(fit,vcov = vcovCL, cluster = ~year_party)
  #  tcofs = data.frame(test[c(2,3),c(1,2)])
    tcofs =  data.frame(summary(fit)$coefficients[c(2,3),c(1,2)])
    tcofs$topic = x
    tcofs
  }
  df2 = df2 %>% group_by(topic) %>% mutate(no =n()) %>% 
    filter(no>20,!is.na(party_share_last),topic!=99,topic >= 1)
  tops<-unique(df2$topic)
  tops = tops[tops!=23]
 # tops = tops[tops<25]
  testFunction <- function (x) {
    return(tryCatch(reg(x), error=function(e) NULL))
  }
  
  mods<-lapply(tops,testFunction)
  #coeffs<-lapply(mods,coeftable)
  #mods2 = do.call(rbind,mods)
  co2<-bind_rows(lapply(mods,data.frame))
  co2$type<-rep(c("stability","overlap"),length(nrow(co2)))
  #co2$topic<-rep(tops,each=2)
  co2
}



bet = data.frame(tcoeffs(dc[dc$cn=="be",]),cn="Belgium")
dkt = data.frame(tcoeffs(dc[dc$cn=="dk",]),cn="Denmark")
de = dc %>% filter(cn=="de")
det = data.frame(tcoeffs(de),cn="Germany")
frt = data.frame(tcoeffs(dc[dc$cn=="fr",]),cn="France")
itt = data.frame(tcoeffs(dc[dc$cn=="it",]),cn="Italy")
nlt = data.frame(tcoeffs(dc[dc$cn=="nl",]),cn="Netherlands")
ukt = data.frame(tcoeffs(dc[dc$cn=="uk",]),cn="United Kingdom")


test = bind_rows(bet,dkt,det,frt,itt,nlt,ukt)


test$topic2 = factor(test$topic)
levels(test$topic2) = c("Economy","Rights","Health","Agriculture","Labour",
                         "Education","Environment","Energy",
                         "Immigration","Transport","Police & justice",
                         "Social policy","Housing","Econ. regulation",
                        "Defense","Science & tech.","Trade","Foreign policy",
                        "State & admin.","Domain","Culture","Reunification")

#png("art/country_topics.png")
test %>% filter(!is.na(topic2),topic!=21,!is.infinite(Std..Error)) %>%
  ggplot(aes(topic2,Estimate,color = type)) + 
  geom_pointrange(aes(ymax = Estimate + Std..Error, 
                      ymin = Estimate - Std..Error),
                  position= position_dodge(width=.5)) +
  coord_flip() + geom_hline(yintercept = 0, linetype =2) +
  labs(x="",y="") + theme_minimal() + 
  scale_color_manual(values = c("black","gray")) +
  theme(legend.title = element_blank(),legend.position = "bottom") +
  facet_wrap(.~cn,nrow = 2)
#dev.off()


```

## Appendix A9: models underlying figure A8

```{r}
dc2 = dc %>% filter(topic >=1 & topic <26 & topic !=21)
dc2$attention_others[is.na(dc2$attention_others)] = 0
dc2$topic2 = factor(dc2$topic)
dc2$topic = as.numeric(as.character(dc2$topic))
levels(dc2$topic2) = c("Economy","Rights","Health","Agriculture","Labour",
                        "Education","Environment","Energy",
                        "Immigration","Transport","Police & justice",
                        "Social policy","Housing","Econ. regulation",
                        "Defense","Science & tech.","Trade","Foreign policy",
                        "State & admin.","Domain","Culture","Reunification")


runmods<-function(df=data,topic = topic,cn=cn,countryn="Country",output=""){
  df2 = df# %>% filter(cn=="BE")
  reg<-function(x) summary(feols(party_share ~ party_share_last + attention_others|year, 
                                data=df2[df2$topic==x,]))
  df2 = df2 %>% group_by(topic) %>% mutate(no =n()) %>% 
    filter(no>20,!is.na(party_share_last),topic!=99,topic >= 1)
  tops<-unique(df2$topic)
  tops = tops[order(tops)]
  # tops = tops[tops<25]
  mods<-lapply(tops,reg)
  lm = length(mods)
  #rowse = data.frame(matrix(nrow=1,ncol=lm+1))
  #rowse[1,] = c("Year (election) fixed effects",rep("+",lm))
  #attr(rowse,'position')=3
  names(mods)=tops
  modelsummary(mods,estimate = "{estimate} ({std.error}){stars}",
               coef_rename = c("party_share_last"="Party share last","attention_others"="Attention other parties"),
               statistic = NULL,#stars=T,
               #add_rows = rowse,
               title = countryn,
               output=output,
               gof_omit = 'DF|Deviance|Std.|FE|AIC|BIC|Within|Pseudo|Log')
}

setwd("art/")
bet = runmods(dc2[dc2$cn=="be",],countryn="Belgium",output="belgium.html")
dkt = runmods(dc2[dc2$cn=="dk",],countryn="Denmark",output="denmark.html")
det = runmods(dc2[dc2$cn=="de",],countryn="Germany",output="germany.html")
frt = runmods(dc2[dc2$cn=="fr",],countryn="France",output="france.html")
itt = runmods(dc2[dc2$cn=="it",],countryn="Italy",output="italy.html")
nlt = runmods(dc2[dc2$cn=="nl",],countryn="Netherlands",output="netherlands.html")
ukt = runmods(dc2[dc2$cn=="uk",],countryn="United Kingdom",output="uk.html")

```

## Figure 3: models with MIPs

```{r}
cns = unique(deb$cn) 
cns = cns[!is.na(cns)]
deb$year_party = paste0(de$year,"_",deb$party)

out = lapply(cns,function(x){
  fit =  lm(party_share ~ scale(party_share_last)+ scale(attention_others) + 
                scale(mip_share),
              #+ scale(share) + factor(topic) 
              data= deb %>% filter(cn == x))
  test = coeftest(fit,vcov = vcovCL, cluster = ~year_party)
  tcofs = data.frame(test[c(2:4),c(1:2)])
  #cofs= data.frame(summary(fit)$coeftable)[,c(1,2,3)]
  cofs=data.frame(vars=rownames(tcofs),tcofs,country=x)
  cofs
})

cns = unique(dc$cn)
cns = cns[cns!="sw"]
dc$year_party = paste0(dc$year,"_",dc$party)

out4 = lapply(cns,function(x){
  fit =  lm(party_share ~ scale(attention_others) + scale(party_share_last),
              #+ scale(share) + factor(topic) 
              data= dc %>% filter(cn == x))
  test = coeftest(fit,vcov = vcovCL, cluster = ~year_party)
  tcofs = data.frame(test[c(2,3),c(1:2)])
  #cofs= data.frame(summary(fit)$coeftable)[,c(1,2,3)]
  cofs=data.frame(vars=rownames(tcofs),tcofs,country=x)
  cofs
})


test = do.call(rbind,out)
test = test %>% filter(vars != "(Intercept)")
test$var2 = c("Stability","Overlap","MIP")

test2 = do.call(rbind,out4)
test2 = test2 %>% filter(vars != "(Intercept)")
test2$var2 = c("Overlap","Stability")
test2 = test2 %>% group_by(country) %>% arrange(desc(var2))

#png("country_mip.png")
test %>% filter(country != "SE") %>%
  ggplot(aes(country,Estimate,color=var2)) + 
  geom_pointrange(aes(ymin=Estimate-Std..Error,ymax=Estimate+Std..Error),
                  position = position_dodge(width=.5)) +
  coord_flip() + geom_hline(yintercept = 0, linetype =2) +
  labs(x="",y="coefficients") + theme_minimal() + 
  scale_color_manual(values = c("gray68","black","gray38")) +
  theme(legend.title = element_blank(),legend.position 
        = "bottom")
#dev.off()
```

## Figure 4: comparing different drivers of overlap

```{r}

allregsF = function(x){
  dn = dc %>% filter(cn == x)  
  fit_oth = lm(party_share ~ party_share_last + attention_others    , data = dn )
  fit_pm = lm(party_share ~ party_share_last + party_share_pm , data = dn )
  fit_cab = lm(party_share ~ party_share_last + cabinet_mean   , data = dn )
  fit_big = lm(party_share ~ party_share_last + party_share_big  , data = dn )
  fit_near = lm(party_share ~ party_share_last + party_share_near  , data = dn )
  out = data.frame(bind_rows(summary(fit_oth)$coefficients[c(3),c(1,2)],
                  summary(fit_pm)$coefficients[c(3),c(1,2)],
                  summary(fit_cab)$coefficients[c(3),c(1,2)],
                  summary(fit_big)$coefficients[c(3),c(1,2)],
                  summary(fit_near)$coefficients[c(3),c(1,2)]))
  out$var = c("Systemic","PM party","Nearest party","Cabinet mean","Biggest party")
  out$cn = x
  out
}

allregsF2 = function(x){
  dn = dc %>% filter(cn == x)  
  dn2 = dc_early |> filter(cn == x)
  fit_oth = lm(party_share ~ party_share_last + attention_others    , data = dn )
  fit_pm = lm(party_share ~ party_share_last + party_share_pm , data = dn )
  fit_cab = lm(party_share ~ party_share_last + cabinet_mean   , data = dn )
  fit_big = lm(party_share ~ party_share_last + party_share_big  , data = dn )
  fit_near = lm(party_share ~ party_share_last + party_share_near  , data = dn )
  fit_first = lm(party_share ~ party_share_last + party_share_early,data =dn2)
  out = data.frame(bind_rows(summary(fit_oth)$coefficients[c(3),c(1,2)],
                             summary(fit_pm)$coefficients[c(3),c(1,2)],
                             summary(fit_cab)$coefficients[c(3),c(1,2)],
                             summary(fit_big)$coefficients[c(3),c(1,2)],
                             summary(fit_near)$coefficients[c(3),c(1,2)],
                             summary(fit_first)$coefficients[c(3),c(1,2)]))
  out$var = c("Systemic","PM party","Nearest party","Cabinet mean","Biggest party","First manifesto")
  out$cn = x
  out
}

fbe = allregsF("be")
fdk = allregsF("dk")
fde = allregsF2("de")
ffr = allregsF2("fr")
fit = allregsF("it")
fnl = allregsF2("nl")
fuk = allregsF2("uk")

out = bind_rows(fbe,fdk,fde,ffr,fit,fnl,fuk)
out$conf.low = out$Estimate-out$Std..Error
out$conf.high = out$Estimate+out$Std..Error
out$country = factor(out$cn)
levels(out$country) = c("Belgium","Germany","Denmark","France","Italy","Netherlands","United Kingdom")
#out$countr = as.character(out$country)

#png("art/coeffs.png",height = 600, width = 600)
out %>% filter(cn!="sw",!(cn=="uk" & var %in% c("Biggest party","Cabinet mean"))) %>% 
  ggplot() + #geom_point() +
  geom_pointrange(aes(country,Estimate, ymin=conf.low,ymax =conf.high,color=var),
                  position = position_dodge(width=.3)) +
  coord_flip() + #scale_color_grey() + 
  theme_minimal() +
  geom_hline(yintercept = 0,linetype=2) + labs(x="",color="") +
  guides(color = guide_legend(reverse=TRUE))
#dev.off()

```

## Appendix A12: Distribution of manifesto publication dates

```{r}


ms2 = ms  |> #filter(Countries != "Netherlands") |>
  dplyr::select(Countries, Elections, `Election date`,`Party name`,manif_date = `Date of manifesto publication`)

ms2$party = factor(ms2$`Party name`)
#levels(ms2$party) = c("U55+","AFD","Gruenen","CDA","CDU","CU","con",
#                     "D66",NA,NA,"GL","lab","PvdA","lib","FDP",
#                    "PC","PDS",NA,"PS","PvdD","PVV","RPR","RPR_UDF",
#                   "SGP","SPD","SP","Die Linke",
#                  NA,"UDF","ukip","Verts","VVD")
ms2$party = as.character(ms2$party)
ms2$`Party name` = NULL
ms2$cn = factor(ms2$Countries)
levels(ms2$cn) = c("FR","DE","NL","UK")
ms2$cn = as.character(ms2$cn)
ms2$Countries = NULL
ms3 = ms2 |> filter(!is.na(manif_date))

ms4 = ms3 |> mutate(date2 = (as.Date(manif_date)),
                    elec_date = as.Date(`Election date`),
                    dist = elec_date - date2, na.rm=T) 
ms4 |>
  ggplot(aes(factor(Elections),dist)) +
  geom_boxplot() +
  coord_flip() + facet_wrap(. ~ cn, scales = "free") +
  theme_minimal() + geom_hline(yintercept = 0, linetype = 2) + 
  scale_y_reverse() + labs(x="",y="")


```

## Appendix A13: Models underlying figure 4

```{r}
allregs = function(x){
  dn = dc %>% filter(cn == x)  
  fit_oth = lm(party_share ~ party_share_last + attention_others    , data = dn )
  fit_pm = lm(party_share ~ party_share_last + party_share_pm , data = dn )
  fit_cab = lm(party_share ~ party_share_last + cabinet_mean   , data = dn )
  fit_big = lm(party_share ~ party_share_last + party_share_big  , data = dn )
  fit_near = lm(party_share ~ party_share_last + party_share_near  , data = dn )
  #fit_first = lm(party_share ~ party_hare_last + party_share_early)
  res = list(fit_oth,fit_pm,fit_near,fit_cab,fit_big)
  res
}

allregs2 = function(x){
  dn = dc %>% filter(cn == x)  
  dn2 = dc_early %>% filter(cn == x)  
  fit_oth = lm(party_share ~ party_share_last + attention_others    , data = dn )
  fit_pm = lm(party_share ~ party_share_last + party_share_pm , data = dn )
  fit_cab = lm(party_share ~ party_share_last + cabinet_mean   , data = dn )
  fit_big = lm(party_share ~ party_share_last + party_share_big  , data = dn )
  fit_near = lm(party_share ~ party_share_last + party_share_near  , data = dn )
  fit_first = lm(party_share ~ party_share_last + party_share_early,data =dn2)
  res = list(fit_oth,fit_pm,fit_near,fit_cab,fit_big,fit_first)
  res
}


regview1 = function(res){
  names(res) = c("Systemic","PM party","Nearest party","Cabinet mean","Biggest party")#,"First manifesto")
  modelsummary(res,
               estimate = "{estimate} ({std.error}){stars}",
               coef_rename = c("(Intercept)"="Constant",
                               "party_share_last"="Party share last",
                               "attention_others"="Attention other parties",
                               "party_share_pm"="PM party",
                               "party_share_near" = "Nearest party",
                               "cabinet_mean" = "Cabinet mean",
                               "party_share_big"="Biggest party"),#,
               # "party_share_early" = "First manifesto"),
               statistic = NULL,#stars=T,
               vcov = vcovCL, cluster = ~year,
               gof_omit = 'DF|Deviance|Std.|FE|AIC|BIC|Within|Pseudo|Log')
}


regview2 = function(res){
  names(res) = c("Systemic","PM party","Nearest party","Cabinet mean","Biggest party","First manifesto")
  modelsummary(res,
               estimate = "{estimate} ({std.error}){stars}",
               coef_rename = c("(Intercept)"="Constant",
                               "party_share_last"="Party share last",
                               "attention_others"="Attention other parties",
                               "party_share_pm"="PM party",
                               "party_share_near" = "Nearest party",
                               "cabinet_mean" = "Cabinet mean",
                               "party_share_big"="Biggest party",
                               "party_share_early" = "First manifesto"),
               statistic = NULL,#stars=T,
               vcov = vcovCL, cluster = ~year,
               gof_omit = 'DF|Deviance|Std.|FE|AIC|BIC|Within|Pseudo|Log')
}

a_be = allregs("be")
a_de = allregs2("de")
a_dk = allregs("dk")
a_fr = allregs2("fr")
a_it = allregs("it")
a_nl = allregs2("nl")
a_uk = allregs2("uk")

regview1(a_be)
regview2(a_de)
regview1(a_dk)
regview2(a_fr)
regview1(a_it)
regview2(a_nl)
regview2(a_uk)

```

## Figure 5: Articles mentioning "wealth tax" and single German parties in Die Welt

```{r}

mw = mw |> mutate(yearmonth = as.Date(yearmonth)) |>
  filter(yearmonth > as.Date("2017-12-01"))

mw2 = mw %>% filter(outlet == "welt") |> dplyr::select(-c(1,10)) |>
  pivot_longer(names_to = "series",values_to = "value",-c(yearmonth)) |>
  mutate(yearmonth = as.Date(yearmonth))
  
mw2$tot = ifelse(mw2$series == "no_articles",1,0)
mw2$labs = NA
mw2$labs[mw2$yearmonth == as.Date("2019-08-01") & mw2$tot == T] = "SPD leader's proposal \nfor Wealth Tax"
mw2$labs[mw2$yearmonth == as.Date("2019-12-01") & mw2$tot == T] = "Proposal accepted \nat SPD party conf."
mw2$labs[mw2$yearmonth == as.Date("2020-12-01") & mw2$tot == T] = "Debate on Wealth \nTax and Pandemic" 
mw2$series[mw2$series=="no_articles"]="total"

cols = c(total="grey",spd="red","cdu"="black","fdp"="yellow","grüne"="green","linke"="darkred")


mw2 |> filter(yearmonth > as.Date("2017-12-01")) |>
  ggplot(aes(yearmonth,value,color=series,label=labs)) + 
  geom_line(alpha=.6) + 
  geom_text_repel(max.overlaps = Inf,nudge_x = .15,
                  segment.curvature = -1,
                  segment.ncp = 3,#box.padding = .5,
                  nudge_y = 2,show.legend = F) + 
  theme_light() + 
  scale_linewidth_manual(values=c(1,2)) +
  scale_color_manual(values = cols) +
  guides(color=guide_legend("serie"), linewidth = "none") +
  theme(legend.title = element_blank()) +
  labs(x="",y="Number of articles")

```

## Appendix A14: media attention to the "wealth tax" in German media outlets

```{r}


mw |> mutate(yearmonth = as.Date(yearmonth)) |>
               select(yearmonth,no_articles,outlet) |>
   ggplot(aes(yearmonth,no_articles ,color=outlet)) + geom_line() +
  theme_light() + labs(x="",y="Number of articles",color="",
                       title = "Level of attention in the FAZ, Handelsblatt and Die Welt") 


mw2 = mw %>% filter(outlet == "hb") |> dplyr::select(-c(1,10)) |>
  pivot_longer(names_to = "series",values_to = "value",-c(yearmonth)) |>
  mutate(yearmonth = as.Date(yearmonth))
mw2$series[mw2$series=="no_articles"]="total"
  

cols = c(total="grey",spd="red","cdu"="black","fdp"="yellow","grüne"="green","linke"="darkred")


mw2 |> filter(yearmonth > as.Date("2017-12-01")) |>
  ggplot(aes(yearmonth,value,color=series)) + 
  geom_line(alpha=.6) + 
  theme_light() + 
  scale_linewidth_manual(values=c(1,2)) +
  scale_color_manual(values = cols) +
  guides(color=guide_legend("serie"), linewidth = "none") +
  theme(legend.title = element_blank()) +
  labs(x="",y="Number of articles",title = "Handelsblatt (German economic daily)")


mw2 = mw %>% filter(outlet == "faz") |> dplyr::select(-c(1,10)) |>
  pivot_longer(names_to = "series",values_to = "value",-c(yearmonth)) |>
  mutate(yearmonth = as.Date(yearmonth))
  

cols = c(total="grey",spd="red","cdu"="black","fdp"="yellow","grüne"="green","linke"="darkred")

mw2$series[mw2$series=="no_articles"]="total"
mw2 |> filter(yearmonth > as.Date("2017-12-01")) |>
  ggplot(aes(yearmonth,value,color=series)) + 
  geom_line(alpha=.6) + 
  theme_light() + 
  scale_linewidth_manual(values=c(1,2)) +
  scale_color_manual(values = cols) +
  guides(color=guide_legend("serie"), linewidth = "none") +
  theme(legend.title = element_blank()) +
  labs(x="",y="Number of articles",title = "Frankfurter Allgemeine Zeitung (Germany daily)")

```

## Appendix A15: sensitivity checks: leaving out 1 topic at the time

```{r}
cns = unique(dc$cn)

res2 =  lapply(cns,function(x){
  dcn = dc |> filter(cn == x)
  tops = unique(dcn$topic)
  lapply(tops,function(x){
    dct = dcn |> filter(topic != x)
    form = party_share ~ party_share_last + attention_others  
    form1 = lm(form,data = dct)
    coeffs = data.frame(summary(form1)$coefficients[c(2:3),c(1,2)])
    coeffs$var = rownames(coeffs)
    coeffs$topic = x
    coeffs$cn = unique(dcn$cn)
    coeffs 
  }) })

res = bind_rows(res2)

res$cn2 = factor(res$cn)
levels(res$cn2) = c("Belgium","Germany","Denmark","France","Italy","Netherlands",
                    "Sweden","United Kingdom")
res$var2 = case_when(res$var == "party_share_last" ~ "Stability",
                     res$var == "attention_others" ~ "Overlap")


#png("art/fig_notopic.png", width = 500, height = 600)
res |> filter(topic != 0.99, cn != "sw") |>
  ggplot(aes(topic,Estimate,color = var2)) +
  geom_pointrange(aes(ymin = Estimate - Std..Error, ymax = Estimate + Std..Error)) +
  theme_light() + facet_wrap(. ~ cn2,scales = "free") + coord_flip() + 
  labs(x = "topic left out", y="",color ="") + 
  theme(legend.position = "bottom")
#dev.off()

```

## Appendix A16: Media attention France and the UK

```{r}



fr1 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_3|year ,~party,
             data = dcf)
fr2 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_6|year ,~party,
             data = dcf)
fr3 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_9|year ,~party,
             data = dcf)
uk1 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_3|year ,~party,
             data = dcu)
uk2 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_6|year ,~party,
             data = dcu)
uk3 =  feols(party_share ~ (party_share_last)+ (attention_others) + topic_9|year ,~party,
             data = dcu)


mods = list(fr1,fr2,fr3,uk1,uk2,uk3)
names(mods) = paste0(rep(c("FR:","UK:"),each=3),c(1:3))
modelsummary(mods,statistic = NULL,
             estimate = "{estimate}({std.error}){stars}",
             gof_omit = "IC|RMSE",
             coef_rename = c("Stability","Overlap","Media 3 months","Media 6 months","Media 9 months"))

```
